'''
Created on Oct 2, 2012

@author: Jason Huang
'''
import binascii
import urllib.request
import urllib.error
import sys

MSG = "f20bdba6ff29eed7b046d1df9fb7000058b1ffb4210a580f748b4ac714c001bd4a61044426fb515dad3f21f18aa577c0bdf302936266926ff37dbf7035d5eeb4"
BASE_URL = "http://crypto-class.appspot.com/po?er="
block_size = 16 #AES block is 128 bit
num_blocks = len(MSG) // 2 // block_size - 1

def solve():
    ans = ''
    for i in range(2, num_blocks):
        msg = MSG[:len(MSG) - i * 2 * block_size]
        ans = solve_block(msg) + ans
    print(ans)

def solve_block(msg):
    ans = []
    
    for pos in range(len(ans), block_size):
        guess = 0
        print("pos = ", pos)
        print("guess = ", end='')
        while guess < 256:
            msg_arr = bytearray.fromhex(msg)
            pad = pos + 1
            for i in range(pos):
                p = -i-1
                msg_arr[p-block_size] ^= ans[p] ^ pad
            msg_arr[-pos - 1 - block_size] ^= guess ^ pad
            url = BASE_URL + binascii.hexlify(msg_arr).decode("utf-8")
            #print(url)
            print(guess, end=" ")
            sys.stdout.flush()
            error_occured = False
            try:
                urllib.request.urlopen(url)
            except urllib.error.URLError as e:
                error_occured = True
                if e.code == 404:
                    print("Good error Found: ", guess, end="")
                    break
            if not error_occured:
                print("No error Found: ", guess, end="")
                break
            guess += 1
        print()
        ans.insert(0, guess)
        print(' '.join([hex(x) for x in ans]))
    s = ''.join([chr(x) for x in ans])
    print(s)
    exit(0)
    return s
    

if __name__ == '__main__':
    solve()